﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.Web;
using System.Security.Cryptography;
using ActivityTrackingLog.Utils;

namespace ActivityTrackingLog.Web.Handlers
{
    internal abstract class BaseHandler : IRequiresSessionState
    {
        private const string _securityKeySessionVariableName = "activity_tracking_security_key";

        public string GetSecurityKey()
        {
            var context = HttpContext.Current;
            if (null != context && null != context.Session)
            {
                string key = string.Empty;
                if (null != context.Session[_securityKeySessionVariableName])
                    key = context.Session[_securityKeySessionVariableName] as string;
                else
                {
                    // generate new key
                    key = context.Session.SessionID.ToSha256Base64();
                    context.Session[_securityKeySessionVariableName] = key;
                }

                return key;
            }

            return null;
        }

        public bool ValidateSecurityKey(string key)
        {
            var result = key == GetSecurityKey();
            return result;
        }

        #region helpers

        #endregion
    }
}
